package com.example.xm.adapter.HomeAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.xm.R;
import com.example.xm.base.BaseAdapter;
import com.example.xm.bean.HomeBean.HomeVideoBean;
import com.example.xm.customizing.PlayVideo;
import com.example.xm.utiles.GildeUtils;
import com.example.xm.utiles.TVUtils;
import com.example.xm.utiles.UserUtils;

import java.util.HashMap;
import java.util.List;

public class PlayVideoAdapter extends BaseAdapter {

    private static final String TAG = "PlayVideoAdapter";
    PlayVideo video;
    private ImageView img_video;

    public PlayVideoAdapter(Context context, List data) {
        super(context, data);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(getLayout(), parent, false);
        BaseViewHolder vh = new BaseViewHolder(view);
        return vh;
    }

    @Override
    protected int getLayout() {
        return R.layout.home_video_activity_item;
    }

    @Override
    protected void bindData(BaseViewHolder vh, Object data) {
        HomeVideoBean.DataBean.ListBean bean = (HomeVideoBean.DataBean.ListBean) data;
        ImageView imghead = (ImageView) vh.getViewById(R.id.img_head);
        GildeUtils.loadRoundImg(context, bean.getHeadUrl(), imghead);
        TextView txtUserName = (TextView) vh.getViewById(R.id.txt_username);
        TVUtils.setText(txtUserName, bean.getNickName());
        TextView txtLive = (TextView) vh.getViewById(R.id.txt_live);
        TVUtils.setText(txtLive, bean.getLikeNumber());
        TextView txtComment = (TextView) vh.getViewById(R.id.txt_comment);
        TVUtils.setText(txtComment, bean.getCommentNumber());
        img_video = (ImageView) vh.getViewById(R.id.iv_video);

        String url = bean.getVideoPath();
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(url, new HashMap<>());
        //获得第1帧图片 这里的第一个参数 以微秒为单位
        Bitmap frameAtTime = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        Log.i(TAG, "bindData: " + frameAtTime);
        retriever.release();
        //添加视频第一帧
        img_video.setImageBitmap(frameAtTime);
        video = (PlayVideo) vh.getViewById(R.id.video);
        if (!TextUtils.isEmpty(bean.getVideoPath())) {
            video.setUp(bean.getVideoPath(), bean.getTitle());
        }
        //用户等级
        int user_lv = UserUtils.getResByLevel(bean.getLevel());
        ImageView imgLv = (ImageView) vh.getViewById(R.id.img_level);
        imgLv.setImageResource(user_lv);
        img_video.setVisibility(View.GONE);
    }

    public void playVideo() {
        if (video != null) {
            video.startButton.performClick();
        }
    }
}
